/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.manager;

import org.openislands.oi.dao.entity.NodeRouteDO;
import org.openislands.oi.dao.mapper.NodeRouteMapper;
import org.openislands.oi.pojo.dto.NodeDTO;
import org.openislands.oi.pojo.dto.RouteDTO;
import org.openislands.oi.util.ValueCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class CommonRouteManager {
    private final GeneralManager<NodeRouteMapper, NodeRouteDO> nodeRouteManager;

    public CommonRouteManager(NodeRouteMapper nodeRouteMapper) {
        this.nodeRouteManager = new GeneralManager<>(nodeRouteMapper);
    }

    public GeneralManager<NodeRouteMapper, NodeRouteDO> getNodeRouteManager() {
        return nodeRouteManager;
    }

    public List<RouteDTO> queryByNodeId(String nodeId) {
        final List<NodeRouteDO> routeDOList = nodeRouteManager.queryAnyList(
                uw -> uw.eq(NodeRouteDO::getNodeId, nodeId).eq(NodeRouteDO::getEnabled, true)
        );
        return ValueCopyUtils.copyList(routeDOList, routeDO -> ValueCopyUtils.copyProperties(routeDO, new RouteDTO()));
    }

    public Boolean addRoute(RouteDTO routeDTO) {
        final NodeRouteDO nodeRouteDO = ValueCopyUtils.copyProperties(routeDTO, new NodeRouteDO(), (s, t) -> {
            t.setEnabled(true);
            t.setTlsEnabled(false);
            return t;
        });

        return nodeRouteManager.save(nodeRouteDO);
    }

    @Transactional
    public Boolean addRoute(List<RouteDTO> routeDTOList) {
        for (RouteDTO routeDTO : routeDTOList) {
            addRoute(routeDTO);
        }
        return true;
    }

    public Boolean deleteRoute(String nodeId) {
        return nodeRouteManager.delete(uw -> uw.eq(NodeRouteDO::getNodeId, nodeId));
    }

    @Transactional
    public Boolean mergeRoute(NodeDTO oldNodeDTO, String updateNodeId) {
        // update route.
        // filter out newly added duplicate information. by HttpAccess
        final Set<String> keySet = new HashSet<>();
        List<RouteDTO> routeDTOList = oldNodeDTO.getRouteDTOList();
        if (Objects.isNull(routeDTOList)) {
            routeDTOList = new ArrayList<>();
        } else {
            for (RouteDTO routeDTO : routeDTOList) {
                routeDTO.setNodeId(updateNodeId);
                keySet.add(routeDTO.getHttpAccess());
            }
        }
        final List<RouteDTO> routeDTOListOld = queryByNodeId(oldNodeDTO.getNodeId());
        for (RouteDTO routeDTO : routeDTOListOld) {
            if (!keySet.contains(routeDTO.getHttpAccess())) {
                routeDTO.setNodeId(updateNodeId);
                routeDTOList.add(routeDTO);
            }
        }
        deleteRoute(oldNodeDTO.getNodeId());
        addRoute(routeDTOList);
        return true;
    }
}
